<?php
namespace Common\Model;

use Think\Model;

class SystemOrderModel extends \BaseModel {
	public function load($order_sn) {
		$info = $this->where("order_sn='$order_sn'")->find();
		if ($info) {
			$info['user'] = BoxModel("system_member")->load($info['user_id']);
			$info['payment'] = BoxModel("SystemPayment")->load($info['payment_id']);
			hook("order_load", $info);
			hook("order_load_" . $info['order_type'], $info);
		}
		return $info;
	}
//支付成功
	public function paySuc($order_sn) {
		$info = $this->load($order_sn);
		$r = $this->where("order_sn='$order_sn'")->save(array('status' => 2, 'pay_time' => date("Y-m-d H:i:s")));
		if ($r) {
			BoxModel("system_money_freeze")->where(array('order_sn' => $order_sn))->save(array('status' => 2));
			hook("order_paySuc", $info);
			hook("order_paySuc_" . $info['order_type'], $info);

		}
		return $r;
	}
//已经发货
	public function logistics($order_sn, $id) {
		$info = $this->where("order_sn='$order_sn'")->find();
		$r = $this->where("order_sn='$order_sn'")->save(array('status' => 4));
		if ($id) {
			$logistics = BoxModel("system_order_logistics")->where(array('id' => $id))->find();
			$info['logistics'] = $logistics;
			hook("order_logistics", $info);
			hook("order_logistics_" . $info['order_type'], $info);
		}
		return $r;
	}
	//取消订单
	public function cancel($order_sn) {
		$info = $this->where("order_sn='$order_sn'")->find();
		if ($info['status'] == 1) {
			$bool = $this->where("order_sn='$order_sn'")->save(array('status' => 3));
			if ($bool) {
				if ($num = $info['card_num']) {
					BoxModel("system_card")->where("num='$num'")->save(array('state' => 2));
				}
				if ($info['yie'] > 0) {
					BoxModel("system_member")->where(array('user_id' => $info['user_id']))->setInc("money", $info['yie']);
					BoxModel("system_money_freeze")->where(array('order_sn' => $order_sn))->save(array('status' => 2));
				}
				hook("order_cancel", $info);
				hook("order_cancel_" . $info['order_type'], $info);
			}

		}

	}

}